package service.impl;

import dao.IOrderDao;
import dao.IUserDao;
import model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.IUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @aythor:侯仪澳
 * @data:2022/8/1
 */
@Service
public class UserService implements IUserService {
    @Autowired
    private IUserDao userDao;

    //查询登录用户和密码
    @Override
    public User queryAllUser(User user) {
        if (user.getuName() == null || user.getuName().isEmpty()) return null;
        if (user.getuPass() == null || user.getuPass().isEmpty()) return null;
        List<User> list = userDao.queryAllUser(user);
        if (list == null || list.size() ==0){
            return null;
        }else {
            return list.get(0);
        }
    }

    //查询一个用户
    @Override
    public User queryOneUser(User user) {
        return userDao.queryOneUser(user);
    }

    //查用户名和手机号是否存在
    @Override
    public String checkUser(User user) {
        User user1 = userDao.checkUser(user);
        if (user1 == null){
            return "0";
        }else {
            return "1";
        }
    }

    //修改用户余额
    @Override
    public String updateUserMoney(User user) {
        User user1 = userDao.queryOneUser(user);
        User user2 = new User();
        //余额的钱加上充值的钱
        user2.setuMoney(user.getuMoney().add(user1.getuMoney()));
        user2.setId(user.getId());
        int count = userDao.updateUserMoney(user2);
        if (count == 1){
            return "1";
        }else {
            return "0";
        }
    }

    @Override
    public String insertOneUser(User user) {

        List<User> list = userDao.queryAllUser(new User(null,user.getuName(),null));
        if (list.size() == 1){
            return "2";
        }
        List<User> list1 = userDao.queryAllUser(new User(null,null,user.getuPhone()));
        if (list1.size() == 1){
            return "3";
        }
        int count = userDao.insertOneUser(user);
        if (count == 1){
            return "1";
        }else {
            return "0";
        }
    }

    //修改用户信息
    @Override
    public String updateUserInfo(User user) {
        int count = userDao.updateUserInfo(user);
        if (count == 1){
            return "1";
        }else {
            return "0";
        }
    }
}
